﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OneShoppingDAL;
using OneShoppingBLL;
using OneShoppingUtility;
using OneShoppingModel;

public partial class Backstage_Pages_sysmng_resultManager : BaseListPage
{
  
    protected void Page_Load(object sender, EventArgs e)
    {

        if (user == null ||role.RoleId >2 || role.RoleId <1)
        {
            UtilityMessageBox.Show(this,"您没有权限操作");
            return;
        }
        PageSize = 10;
        Condition = " ";
        TableName = "ActivityResult"; Pk = "ResultId";

        if (!this.IsPostBack)
        {
            
            if (role.RoleId == 2)
            {
                Condition = Condition + "and TradeId in (";
                List<activitypurchasingView> list = activitypurchasingViewBLL.GetInstance().GetList(r => r.SellerId == user.UserId);
                //待写根据登入的用户查询条件
                //下面根据用户名查询，未发货，已发货的也要写相应的条件
                foreach (var l in list)
                {
                    Condition = Condition + l.TradeId + " ,";
                }
                Condition = Condition.Substring(0, Condition.Length - 1);
                Condition = Condition + ")";
                btnSearch.Visible = false;
                textName.Visible = false;
                TextBox1.Visible = false;
            }
            PageIndex = 0;
            BindData(ref rptList);
            BindAspNetPager(ref Pager);
        }
    }
    protected void Pager_PageChanged(object sender, EventArgs e)
    {
        PageIndex = Pager.CurrentPageIndex - 1;
        BindData(ref rptList);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        //查询
        string name = textName.Text.Trim();
        try
        {
            if (name != "")
            {
                int id = SystemUserBLL.GetInstance().GetEntity(r => r.LoginName == name).UserId;
                Condition = Condition + "and TradeId in (";
                if (role.RoleId == 1)
                {
                    List<PurchasingList> list = PurchasingListBLL.GetInstance().GetList(r => r.UserId == id);
                    foreach (var l in list)
                    {
                        Condition = Condition + l.TradeId + " ,";
                    }
                }
                if (role.RoleId == 2)
                {
                    List<activitypurchasingView> list = activitypurchasingViewBLL.GetInstance().GetList(r => r.SellerId == user.UserId && r.UserId == id);
                    //待写根据登入的用户查询条件
                    //下面根据用户名查询，未发货，已发货的也要写相应的条件
                    foreach (var l in list)
                    {
                        Condition = Condition + l.TradeId + " ,";
                    }
                }
                Condition = Condition.Substring(0, Condition.Length - 1);
                Condition = Condition + ")";
            }
            else
            {
                if (role.RoleId == 2)
                {
                    Condition = Condition + "and TradeId in (";
                    List<activitypurchasingView> list = activitypurchasingViewBLL.GetInstance().GetList(r => r.SellerId == user.UserId );
                    foreach (var l in list)
                    {
                        Condition = Condition + l.TradeId + " ,";
                    }
                    Condition = Condition.Substring(0, Condition.Length - 1);
                    Condition = Condition + ")";
                }
            }
        }
        catch
        {
            UtilityMessageBox.Show(this, "输入用户名不存在");
        }
        PageIndex = 0;//新定位到查询集合的第一页
        BindData(ref rptList);
        BindAspNetPager(ref Pager);
    }

    public string getUserName(object o)
    {
        String result = "异常";
        try
        {
            int id = Convert.ToInt32(o);
            int listId = PurchasingListBLL.GetInstance().GetEntity(r => r.TradeId == id).ListId;
            PurchasingList pList = PurchasingListBLL.GetInstance().GetEntity(r => r.ListId == listId);
            result = SystemUserBLL.GetInstance().GetEntity(r => r.UserId == pList.UserId).LoginName;
        }
        catch { }
        return result;
    }

    public string getStatus(object o)
    {
        string result = "异常";
        try
        {
            int id = Convert.ToInt32(o);
            switch (id)
            {
                case 0: result = "待填收货地址"; break;
                case 1: result = "待发货"; break;
                case 2: result = "已发货"; break;
                case 3: result = "已收货"; break;
            }
        }
        catch { }
        return result;
    }

    public string getOperation(object o, object oo, object phone)
    {
        string result = "异常";
        try
        {
            int resultId = Convert.ToInt32(o);
            int status = Convert.ToInt32(oo);
            switch (status)
            {
                case 0:
                    result = "用户联系电话：" + phone;
                    break;
                case 1:
                    if (role.RoleId == 2)
                    {
                        result = "<a href='resultSendGoods.aspx?resultId=" + resultId + "'>发货</a>";
                    }
                    else
                    {
                        result = "<a href='#'>待发货</a>";
                    }

                    break;
                case 2:
                    result = "无";
                    break;
                case 3:
                    result = "无";
                    break;
            }
        }
        catch { }
        return result;
    }

    public string getListId(object o)
    {
        string result = "异常";
        try
        {
            int id = Convert.ToInt32(o);
            result = PurchasingListBLL.GetInstance().GetEntity(r => r.TradeId == id).ListId.ToString();
        }
        catch { }
        return result;
    }
    protected void Button11_Click(object sender, EventArgs e)
    {
        Condition = "";
        if (role.RoleId == 1)
        {
            Condition = Condition + "and Status in ( 2 , 3 )";
        }
        else if (role.RoleId == 2)
        {
            Condition = Condition + " and Status in ( 2 , 3 ) and TradeId in (";
            List<activitypurchasingView> list = activitypurchasingViewBLL.GetInstance().GetList(r => r.SellerId == user.UserId);
            foreach (var l in list)
            {
                Condition = Condition + l.TradeId + " ,";
            }
            Condition = Condition.Substring(0, Condition.Length - 1);
            Condition = Condition + ")";
        }
        PageIndex = 0;//新定位到查询集合的第一页
        BindData(ref rptList);
        BindAspNetPager(ref Pager);
    }
    protected void Button22_Click(object sender, EventArgs e)
    {
        Condition = "";
        if (role.RoleId == 1)
        {
            Condition = Condition + "and Status in ( 0 , 1 )";
        }
        else if (role.RoleId == 2)
        {
            Condition = Condition + " and Status in ( 0 , 1 ) and TradeId in (";
            List<activitypurchasingView> list = activitypurchasingViewBLL.GetInstance().GetList(r => r.SellerId == user.UserId );

            foreach (var l in list)
            {
                Condition = Condition + l.TradeId + " ,";
            }
            Condition = Condition.Substring(0, Condition.Length - 1);
            Condition = Condition + ")";
        }
        PageIndex = 0;//新定位到查询集合的第一页
        BindData(ref rptList);
        BindAspNetPager(ref Pager);
    }

    public int getDone()
    {
        return getStatusNum("2,3");
    }

    public int getTodo()
    {
        return getStatusNum("0,1");
    }

    private int getStatusNum(string status)
    {
        int result = 0;
        try
        {
            string cond = "";
            if (role.RoleId == 1)
            {
                cond = " and Status in ( "+status+" ) ";
            }
            else if (role.RoleId == 2)
            {
                cond = " and Status in ( " + status + " ) and TradeId in (";
                List<activitypurchasingView> list = activitypurchasingViewBLL.GetInstance().GetList(r => r.SellerId == user.UserId);
                foreach (var l in list)
                {
                    cond = cond + l.TradeId + " ,";
                }
                cond = cond.Substring(0, cond.Length - 1);
                cond = cond + ")";
            }
            result = SqlUtility.GetInstance().CommonSelectDataCountByCondition("ActivityResult", cond);
        }
        catch (Exception e)
        {
            result = 0;
        }
        return result;
    }
}